﻿/*********************************************************************************
**                                                                              **
**  Copyright (C) 2024 LiLong                                                   **
**  This file is part of GnssLogs.                                              **
**                                                                              **
**  GnssLogs is free software: you can redistribute it and/or modify            **
**  it under the terms of the GNU Lesser General Public License as published by **
**  the Free Software Foundation, either version 3 of the License, or           **
**  (at your option) any later version.                                         **
**                                                                              **
**  GnssLogs is distributed in the hope that it will be useful,                 **
**  but WITHOUT ANY WARRANTY; without even the implied warranty of              **
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               **
**  GNU Lesser General Public License for more details.                         **
**                                                                              **
**  You should have received a copy of the GNU Lesser General Public License    **
**  along with GnssLogs.  If not, see <https://www.gnu.org/licenses/>.          **
**********************************************************************************/
#pragma once

#include "../GSA.h"
#include "Commons.h"

namespace GnssLogs
{
namespace GSADecoder
{
using namespace tao::pegtl;
using LogHeader = NMEA_TALKER_IDENTIFIER("GSA");
using MAMode    = one<'A', 'M'>;
using Mode123   = one<'1', '2', '3'>;
// clang-format off
struct PRN : Floating {};
struct PDOP : Floating {};
struct HDOP : Floating {};
struct VDOP : Floating {};
// clang-format on
using Expression = seq<LogHeader,
                       Split,
                       MAMode,
                       Split,
                       Mode123,
                       rep<12, Split, opt<PRN>>,
                       Split,
                       PDOP,
                       Split,
                       HDOP,
                       Split,
                       VDOP,
                       opt<Split, SystemId>,
                       one<'*'>,
                       CheckSum,
                       Tail>;

template<typename T>
struct Action
{
};

template<>
struct Action<LogHeader>
{
    template<typename ParseInput>
    static bool apply(ParseInput& in, GSA::Data& data)
    {
        data.header = std::string(in.begin(), in.size());
        return true;
    }
};

template<>
struct Action<MAMode>
{
    template<typename ParseInput>
    static bool apply(ParseInput& in, GSA::Data& data)
    {
        data.maMode = *in.begin() == 'A' ? GSA::Data::MAMode::Automatic : GSA::Data::MAMode::Manual;
        return true;
    }
};

template<>
struct Action<Mode123> : IntHelper<Action<Mode123>, uint8_t>
{
    static void setValue(GSA::Data& data, uint8_t value) { data.mode123 = static_cast<GSA::Data::Mode123>(value); }
};

template<>
struct Action<PRN> : IntHelper<Action<PRN>, uint8_t>
{
    static void setValue(GSA::Data& data, uint8_t value) { data.prns.push_back(value); }
};

template<>
struct Action<PDOP> : DoubleHelper<Action<PDOP>>
{
    static void setValue(GSA::Data& data, double value) { data.pdop = value; }
};

template<>
struct Action<HDOP> : DoubleHelper<Action<HDOP>>
{
    static void setValue(GSA::Data& data, double value) { data.hdop = value; }
};

template<>
struct Action<VDOP> : DoubleHelper<Action<VDOP>>
{
    static void setValue(GSA::Data& data, double value) { data.vdop = value; }
};

template<>
struct Action<SystemId> : SystemIdHelper<Action<SystemId>>
{
    static void setValue(GSA::Data& data, uint8_t value) { data.systemId = value; }
};

template<>
struct Action<CheckSum> : CheckSumHelper<Action<CheckSum>>
{
    static void setValue(GSA::Data& data, uint8_t value) { data.checkSum = value; }
};
} // namespace GSADecoder
} // namespace GnssLogs